package com.doge.walknovel.business.novel.reply

import android.os.Bundle
import android.view.*
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.doge.wnpro.R
import com.doge.walknovel.api.model.CommentInfo
import com.doge.walknovel.core.BaseDialogFragment
import com.doge.walknovel.core.base.adapters.ITEM_CLICKED
import com.doge.walknovel.core.base.isRefreshing
import com.doge.wnpro.databinding.DialogFragmentReplyBinding
import com.doge.walknovel.utils.screenHeight

class ReplyDialogFragment : BaseDialogFragment() {
    private var innerBinding: DialogFragmentReplyBinding? = null
    private val binding get() = innerBinding!!
    override val hasShadow = true
    private val commentId by lazy { arguments?.getInt(EXTRA_COMMENTS_ID)!! }
    private val viewModel: ReplyViewModel by viewModels { ReplyViewModelFactory(commentId) }
    private val adapter by lazy { ReplyAdapter() }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        innerBinding =
            innerBinding ?: DialogFragmentReplyBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.recyclerView.itemAnimator = null
        binding.recyclerView.layoutManager = LinearLayoutManager(binding.recyclerView.context)
        binding.recyclerView.adapter = adapter
        adapter.setListener(::handleAdapterCallback)
        binding.refreshLayout.setColorSchemeResources(R.color.pull_refresh)
        binding.refreshLayout.setOnRefreshListener {
            viewModel.requestCommentReplies()
        }

        viewModel.loading.observe(viewLifecycleOwner) {
            binding.refreshLayout.isRefreshing = it.isRefreshing()
        }
        viewModel.dataList.observe(viewLifecycleOwner) {
            adapter.submitList(it)
        }
        viewModel.requestCommentReplies()
    }

    private fun handleAdapterCallback(action: String, item: Any?) {
        when (action) {
            ITEM_CLICKED -> viewModel.requestCollectReply(item as? CommentInfo?)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding.refreshLayout.setOnRefreshListener(null)
        binding.recyclerView.adapter = null
        adapter.setListener(null)
        innerBinding = null
    }

    override fun setWindow(win: Window) {
        win.setWindowAnimations(R.style.Animation_Bottom_Rising)
        win.decorView.setPadding(0, 0, 0, 0)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = (screenHeight * 0.6f).toInt()
        win.attributes = lp
        win.setGravity(Gravity.BOTTOM)
    }

    companion object {
        private const val EXTRA_COMMENTS_ID = "commentId"
        fun newInstance(commentId: Int): ReplyDialogFragment {
            return ReplyDialogFragment().apply {
                arguments = Bundle().apply {
                    putInt(EXTRA_COMMENTS_ID, commentId)
                }
            }
        }
    }

}